package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.dto.DeploymentDto;
import com.csun.cmny.provider.model.constant.MedicConstant;
import com.csun.cmny.provider.model.constant.OldOutConstant;
import com.csun.cmny.provider.model.constant.ReviseConstant;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcActivitiService;
import com.csun.cmny.provider.service.OmcOldOutVarService;
import com.csun.cmny.provider.service.OmcReviseVarService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Maps;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-29 09:24
 * @Description: the class omc activiti controller
 */
@RestController
@RequestMapping(value = "/activiti", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcActivitiController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcActivitiService omcActivitiService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private OmcReviseVarService omcReviseVarService;
    @Resource
    private OmcOldOutVarService omcOldOutVarService;
    /**
     * 部署流程
     *
     * @param deploymentDto deploy dto
     * @return
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/createDeploy", method = RequestMethod.POST)
    public Wrapper createDeployment(@Validated @RequestBody DeploymentDto deploymentDto) {

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(deployment);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

    /**
     * 删除流程部署
     *
     * @param processKey    process key
     * @return
     */
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/deleteDeploy", method = RequestMethod.POST)
    public Wrapper deleteDeployment(@RequestParam("processKey") String processKey,
                                    @RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(processKey, groupId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    @RequestMapping(value = "/orderProcessShow", method = RequestMethod.POST)
    public Wrapper orderProcessDeploymentShow(@RequestParam Long groupId) {

        if (groupId == null) {
            groupId = getLoginAuthDto().getGroupId();
        }

        // 校验流程是否已经部署
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(Constant.PROCESS_KEY_ORDER)        // 流程key
                .processDefinitionTenantId(groupId.toString())  // 流程租户
                .singleResult();

        if (processDefinition == null) {
            return WrapMapper.ok(null);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("name", processDefinition.getName());
        map.put("version", processDefinition.getVersion());
        map.put("key", processDefinition.getKey());
        map.put("resourceName", processDefinition.getResourceName());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), map);
    }


    @RequestMapping(value = "/orderProcessCreate", method = RequestMethod.POST)
    public Wrapper orderProcessDeploymentCreate(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(Constant.PROCESS_KEY_ORDER);
        deploymentDto.setDeploymentName("服务订单流程");
        deploymentDto.setDeploymentResource("activiti/OrderServiceProcess.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

    @RequestMapping(value = "/orderProcessDelete", method = RequestMethod.POST)
    public Wrapper orderProcessDeploymentDelete(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(Constant.PROCESS_KEY_ORDER, groupId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    @RequestMapping(value = "/medicPlanProcessShow", method = RequestMethod.POST)
    public Wrapper medicPlanProcessDeploymentShow(@RequestParam Long groupId) {

        if (groupId == null) {
            groupId = getLoginAuthDto().getGroupId();
        }

        // 校验流程是否已经部署
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(MedicConstant.PROCESS_KEY_MEDIC_PLAN)        // 流程key
                .processDefinitionTenantId(groupId.toString())  // 流程租户
                .singleResult();

        if (processDefinition == null) {
            return WrapMapper.ok(null);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("name", processDefinition.getName());
        map.put("version", processDefinition.getVersion());
        map.put("key", processDefinition.getKey());
        map.put("resourceName", processDefinition.getResourceName());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), map);
    }

   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/medicPlanProcessCreate", method = RequestMethod.POST)
    public Wrapper medicPlanProcessDeploymentCreate(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(MedicConstant.PROCESS_KEY_MEDIC_PLAN);
        deploymentDto.setDeploymentName("用药计划流程");
        deploymentDto.setDeploymentResource("activiti/medicPlan.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/medicPlanProcessDelete", method = RequestMethod.POST)
    public Wrapper medicPlanProcessDeploymentDelete(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(MedicConstant.PROCESS_KEY_MEDIC_PLAN, groupId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    @RequestMapping(value = "/medicRecordProcessShow", method = RequestMethod.POST)
    public Wrapper medicRecordProcessDeploymentShow(@RequestParam Long groupId) {

        if (groupId == null) {
            groupId = getLoginAuthDto().getGroupId();
        }

        // 校验流程是否已经部署
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(MedicConstant.PROCESS_KEY_MEDIC_RECORD)        // 流程key
                .processDefinitionTenantId(groupId.toString())  // 流程租户
                .singleResult();

        if (processDefinition == null) {
            return WrapMapper.ok(null);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("name", processDefinition.getName());
        map.put("version", processDefinition.getVersion());
        map.put("key", processDefinition.getKey());
        map.put("resourceName", processDefinition.getResourceName());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), map);
    }

   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/medicRecordProcessCreate", method = RequestMethod.POST)
    public Wrapper medicRecordProcessDeploymentCreate(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(MedicConstant.PROCESS_KEY_MEDIC_RECORD);
        deploymentDto.setDeploymentName("用药记录流程");
        deploymentDto.setDeploymentResource("activiti/medicRecord.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/medicRecordProcessDelete", method = RequestMethod.POST)
    public Wrapper medicRecordProcessDeploymentDelete(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(MedicConstant.PROCESS_KEY_MEDIC_RECORD, groupId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    @RequestMapping(value = "/reviseProcessShow", method = RequestMethod.POST)
    public Wrapper reviseProcessDeploymentShow(@RequestParam Long groupId) {

        if (groupId == null) {
            groupId = getLoginAuthDto().getGroupId();
        }

        // 校验流程是否已经部署
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(ReviseConstant.PROCESS_KEY_REVISE)        // 流程key
                .processDefinitionTenantId(groupId.toString())  // 流程租户
                .singleResult();

        if (processDefinition == null) {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("name", processDefinition.getName());
        map.put("version", processDefinition.getVersion());
        map.put("key", processDefinition.getKey());
        map.put("resourceName", processDefinition.getResourceName());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), map);
    }

    /**
     * 调房调级流程添加
     *
     * @param groupId   the group id
     *
     * @return  the wrapper
     */
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/reviseProcessCreate", method = RequestMethod.POST)
    public Wrapper reviseProcessDeploymentCreate(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(ReviseConstant.PROCESS_KEY_REVISE);
        deploymentDto.setDeploymentName("调房调级流程");
        deploymentDto.setDeploymentResource("activiti/ReviseProcess.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // 批量插入调房调级变量
        omcReviseVarService.init(getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

    /**
     * 调房调级流程删除
     *
     * @return
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/reviseProcessDelete", method = RequestMethod.POST)
    public Wrapper reviseProcessDeploymentDelete(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(ReviseConstant.PROCESS_KEY_REVISE, groupId);

        // 批量删除调房调级变量
        omcReviseVarService.destroy(getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    /**
     * 校验出院流程是否部署
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/oldOutProcessShow", method = RequestMethod.POST)
    public Wrapper oldOutProcessShow(@RequestParam Long groupId) {

        if (groupId == null) {
            groupId = getLoginAuthDto().getGroupId();
        }

        // 校验流程是否已经部署
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(OldOutConstant.PROCESS_KEY_OLD_OUT)        // 流程key
                .processDefinitionTenantId(groupId.toString())  // 流程租户
                .singleResult();

        if (processDefinition == null) {
            return WrapMapper.ok(null);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("name", processDefinition.getName());
        map.put("version", processDefinition.getVersion());
        map.put("key", processDefinition.getKey());
        map.put("resourceName", processDefinition.getResourceName());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), map);
    }

    /**
     * 出院流程添加
     *
     * @param groupId   the group id
     *
     * @return  the wrapper
     */
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/oldOutProcessCreate", method = RequestMethod.POST)
    public Wrapper oldOutProcessCreate(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(OldOutConstant.PROCESS_KEY_OLD_OUT);
        deploymentDto.setDeploymentName("出院流程");
        deploymentDto.setDeploymentResource("activiti/OldOutProcess.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, getLoginAuthDto());
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // 批量插入出院流程变量
        omcOldOutVarService.init(getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), deployment);
    }

    /**
     * 出院流程删除
     *
     * @return
     */
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/oldOutProcessDelete", method = RequestMethod.POST)
    public Wrapper oldOutProcessDelete(@RequestParam Long groupId) {

        if (groupId != null) {
            getLoginAuthDto().setGroupId(groupId);
        }

        omcActivitiService.deleteDeployment(OldOutConstant.PROCESS_KEY_OLD_OUT, groupId);

        // 批量删除出院流程变量
        omcOldOutVarService.destroy(getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

}
